﻿@using si.caha.Helper;

@{
    var item = PageData["item"];
    var disabledAttribute = "";
    
    if(Validator.isCookieFilled(Request.Cookies) && Request.Cookies["cart"]["items"].Contains(item.ItemId.ToString()))
    {
        disabledAttribute = "disabled";
    }
}
<script>
    $(function(){
        $("#addToCart").click(function(){
            // ajax post
            $.post("/Cart/partial/addToCart.cshtml", { itemId: $("#itemId").val()}, 
                // success
                function(){
                    $("button#addToCart").html("Added");
                    $("button#addToCart").attr("disabled", true);
                    var cartItemsCount = parseInt($("#cartItemsCount").html());
                    cartItemsCount++;
                    $("#cartItemsCount").html(cartItemsCount);
            });
        });
    });
</script>

<p>@item.Title</p>
@foreach (var image in item.Images)
{
    <img src="@Href(@image.Path)" />
}
<p>@item.Description</p>
<p>€@item.Price</p>
@if (disabledAttribute == "disabled")
{
    <a href="@Href("~/Cart")">Item already in cart</a>
}
<input type="hidden" id="itemId" value="@item.ItemId" />
<button id="addToCart" type="submit" @disabledAttribute>Add to cart</button>